<!--
  * 计费规则
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="计费类型" class="smart-query-form-item">
        <a-select v-model:value="queryForm.type" style="width: 180px" allowClear :options="BILLING_TYPE" placeholder="请选择" />
      </a-form-item>
      <a-form-item label="结算模式" class="smart-query-form-item">
        <a-select v-model:value="queryForm.settleType" style="width: 180px" allowClear :options="SETTLE_TYPE" placeholder="请选择" />
      </a-form-item>
      <a-form-item label="计费模式" class="smart-query-form-item">
        <a-select v-model:value="queryForm.chargeType" style="width: 180px" allowClear :options="CHARGE_TYPE" placeholder="请选择" />
      </a-form-item>
      <a-form-item label="计费对象" class="smart-query-form-item">
        <a-select v-model:value="queryForm.chargeUserType" style="width: 180px" allowClear :options="CHARGE_USER_TYPE" placeholder="请选择" />
      </a-form-item>
      <!-- <a-form-item label="代理商账号" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.no" placeholder="输入账号" />
      </a-form-item>
      <a-form-item label="状态" class="smart-query-form-item">
        <SmartEnumSelect width="200px" v-model:value="queryForm.status" enum-name="STATUS_ENUM" placeholder="状态" />
      </a-form-item> -->
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="onSearch">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-title">数据列表</div>
      <!-- <div class="smart-table-operate-block">
      </div> -->
      <div class="smart-table-setting-block">
        <a-button @click="showForm" type="primary" size="small" v-privilege="'charge:insert'">
          <template #icon>
            <PlusOutlined />
          </template>
          新增
        </a-button>
        <!-- <TableOperator v-model="columns" :tableId="null" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ text, record, column }">
        <template v-if="column.dataIndex === 'status'">
          <a-switch
            v-privilege="'charge:update_status'"
            :checked="text"
            :checkedValue="1"
            :unCheckedValue="0"
            @change="(checked) => handleStatusChange(checked, record)"
            :loading="record.statusLoading"
          />
        </template>
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="showForm(record)" type="link" v-privilege="'charge:update'">修改</a-button>
            <a-button @click="deleteRow(record)" type="link" v-privilege="'charge:delete'">删除</a-button>
          </div>
        </template>
      </template>
    </a-table>

    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :page_sizeOptions="PAGE_SIZE_OPTIONS"
        :defaultpage_size="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>

    <ChargeForm ref="formRef" @ok="queryData" v-model:open="openForm" :formType="chargeFormType" :formId="chargeFormId" />
  </a-card>
</template>
<script setup name="Charge">
  import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
  import { message, Modal } from 'ant-design-vue'
  import { chargePage, chargeDelete, chargeUpdateStatus } from '/@/api/business/b-operation/channel/charge-api.js'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import ChargeForm from './charge-form.vue'
  import dayjs from 'dayjs'
  // import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue'
  import { selectDictLabel } from '/@/utils/common'
  import {
    BILLING_TYPE, //计费类型
    CHARGE_USER_TYPE, //	计费对象
    CHARGE_TYPE, //计费模式
    SETTLE_TYPE, //'结算模式'
  } from '/@/constants/dict'
  import { isEmpty } from 'lodash'

  const { proxy } = getCurrentInstance()

  // ---------------------------- 表格列 ----------------------------
  const columns = ref([
    // {
    //   title: '序号',
    //   dataIndex: 'index',
    //   customRender: ({ index }) => {
    //     return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
    //   },
    // },
    {
      title: '计费编号',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '计费类型',
      dataIndex: 'type',
      customRender: ({ text }) => {
        return selectDictLabel(BILLING_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '计费名称',
      dataIndex: 'title',
      ellipsis: true,
    },
    {
      title: '结算模式',
      dataIndex: 'settleType',
      customRender: ({ text }) => {
        return selectDictLabel(SETTLE_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '计费模式',
      dataIndex: 'chargeType',
      customRender: ({ text }) => {
        return selectDictLabel(CHARGE_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '计费对象',
      dataIndex: 'chargeUserType',
      customRender: ({ text }) => {
        return selectDictLabel(CHARGE_USER_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '创建时间',
      dataIndex: 'cTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      ellipsis: true,
    },
    {
      title: '更新时间',
      dataIndex: 'uTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      ellipsis: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 250,
    },
  ])
  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    type: undefined, //计费类型
    settleType: undefined, //结算类型
    chargeType: undefined, //计费模式
    chargeUserType: undefined, //计费用户类型
    sortFields:[{
      field:'c_time',
      order:'desc'
    }],
    pageNo: 1,
    pageSize: 10,
  }

  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await chargePage(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }

  onMounted(queryData)

  function deleteRow(record) {
    Modal.confirm({
      title: '确定要删除如下数据吗?',
      content: `计费名称:${record.title}`,
      okText: '删除',
      okType: 'danger',
      onOk() {
        chargeDelete({ id: record.id }).then(() => {
          queryData()
        })
      },
      cancelText: '取消',
      onCancel() {},
    })
  }
  // 修改状态
  function handleStatusChange(checked, record) {
    Modal.warning({
      title: '确定要修改如下状态吗?',
      content: `计费名称:${record.title}`,
      okText: '确定',
      onOk() {
        chargeUpdateStatus({ id: record.id, status: checked }).then(() => {
          message.success('修改成功')
          queryData()
        })
      },
      cancelText: '取消',
      onCancel() {},
    })
    console.log(checked, record)
  }
  // ---------------------------- 添加/修改 ----------------------------
  const formRef = ref()
  const openForm = ref(false)
  const chargeFormType = ref()
  const chargeFormId = ref()

  function showForm(row) {
    openForm.value = true
    chargeFormType.value = isEmpty(row) ? 'add' : 'update'
    chargeFormId.value = isEmpty(row) ? '' : row.id
  }
</script>
